const router = require("koa-router")();
const FeeModel = require("../models/fee.js");
const RoomModel = require("../models/room.js");
const HouseModel = require("../models/house.js");
const ResultHelper = require("../utils/resultHelper.js");
const VerifyHelper = require("../utils/verifyHelper.js");
const formatDateHelper = require('../utils/formatDateHelper.js')


router.get("/fees", async ctx => {
	let {
		page,
		limit,
		number,
		time,
		house_id,
		room_id,
		status,
		id
	} = ctx.query;
	if (page == undefined) {
		page = 0
	}
	if (limit == undefined) {
		limit = 10
	}
	const query = {};


	// 构建查询条件
	if (number) {
		query.number = {
			$regex: ctx.query.number
		}; // 不区分大小写的正则表达式
	}

	if (time) {
		query.time = {
			$regex: ctx.query.time
		}; // 不区分大小写的正则表达式
	}
	if (house_id != undefined && house_id !== '0') {
		query.house_id = house_id;
	}


	if (status != undefined && status !== '0') {
		query.status = status;
	}

	if (room_id != undefined && room_id !== '0') {
		query.room_id = room_id;
	}

	if (id != undefined && id !== '0') {
		query.id = id;
	}


	var data = await FeeModel.find(query).skip(Number(page - 1) * limit).limit(Number(limit));
	var total = await FeeModel.find(query).countDocuments();

	const enhancedData = await Promise.all(data.map(async fee => {
		const room = await RoomModel.findOne({
			id: fee.room_id
		});
		const house = await HouseModel.findOne({
			id: fee.house_id
		});
		

		return {
			...fee.toObject(),
			room,
			house
		};
	}));
	ctx.body = {
		code: 200,
		data: enhancedData,
		total,
		msg: "ok"
	}

})

router.post("/fee", async ctx => {
	VerifyHelper.token(ctx)
	const {
		id,
		time,
		house_id,
		room_id,
		water_unit,
		water,
		electricity_unit,
		electricity,
		comprehensive,
		sum_total,
		status,
		create,
		modify
	} = ctx.request.body;

	if (!id) {
		if (!house_id) {
			return ResultHelper.errInfo422(ctx, "请选择单元楼")
		}
		
		if (!room_id) {
			return ResultHelper.errInfo422(ctx, "请选择房间号")
		}
		const carPost = new FeeModel({
			id,
			time,
			house_id,
			room_id,
			water_unit,
			water,
			electricity_unit,
			electricity,
			comprehensive,
			sum_total,
			status: 1,
			create,
			modify
		});
		const res = await carPost.save();
		ctx.body = {
			code: 200,
			msg: "添加成功"
		}
	} else {
		// 已存在数据
		// 获取下数据的内容
		const item = await FeeModel.findOne({
			id: ctx.request.body.id
		});
		const res = await FeeModel.updateOne({
			id: id
		}, {
			$set: {
				id,
				time: ctx.request.body.time ? ctx.request.body.time : item.time,
				house_id: ctx.request.body.house_id ? ctx.request.body.house_id : item.house_id,
				room_id: ctx.request.body.room_id ? ctx.request.body.room_id : item.room_id,
				water_unit: ctx.request.body.water_unit ? ctx.request.body.water_unit : item
					.water_unit,
				water: ctx.request.body.water ? ctx.request.body.water : item.water,
				electricity_unit: ctx.request.body.electricity_unit ? ctx.request.body
					.electricity_unit : item.electricity_unit,
				electricity: ctx.request.body.electricity ? ctx.request.body.electricity : item
					.electricity,
				comprehensive: ctx.request.body.comprehensive ? ctx.request.body.comprehensive :
					item.comprehensive,
				sum_total: ctx.request.body.sum_total ? ctx.request.body.sum_total : item.sum_total,
				status: ctx.request.body.status ? ctx.request.body.status : item.status,
				create: ctx.request.body.create ? ctx.request.body.create : item.create,
				modify: formatDateHelper.date()
			}
		});
		ctx.body = {
			code: 200,
			msg: "编辑成功"
		}


	}
})


module.exports = router;